#!/usr/bin/env ruby
# unixbench.workload is defined as the total operations in unixbench for
# all processes/threads

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/log"
require "#{LKP_SRC}/lib/string_ext"
require 'time'

# Benchmark run time format displayed as below:
# Benchmark Run: Tue Jan 01 2019 15:48:24 - 15:56:36

while (line = $stdin.gets)
  line = line.resolve_invalid_bytes

  case line.chomp!
  when /^Benchmark Run: .+ (\d+:\d+:\d+) - (\d+:\d+:\d+)$/
# We only care about the difference value between end time and start time
    start_time = Time.parse($1).to_f
    end_time = Time.parse($2).to_f
    duration = if end_time > start_time
                 end_time - start_time
               else
                 end_time + 3600 * 24 - start_time # date overflo
               end
  when /^System Benchmarks Partial Index .* INDEX$/,
       /^System Benchmarks Index Values(\s+\S+){3}$/
    index_list = []
    while (line = STDIN.gets)
      lines = line.split
      lines_l = lines.length
      break if lines_l < 4

      index = lines[-1]
      index_value = lines[0, lines_l - 3].join('_')
      index_list.append("#{index_value}: #{index}")
    end
  when /^System Benchmarks Index Score \s+ ([0-9.]+)$/
    score = $1
  end
end

if index_list.nil?
  puts 'incomplete_result: 1'
else
  index_list.each do |index|
    puts index.to_s
  end
  puts "score: #{score}" if score
end
